from langchain.chains.question_answering.map_rerank_prompt import output_parser
from langchain_core.output_parsers import JsonOutputParser,CommaSeparatedListOutputParser
from langchain_core.prompts import ChatPromptTemplate,PromptTemplate
from langchain_openai import  ChatOpenAI

#模型
mode = ChatOpenAI(model_name="qwen-plus",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key="sk-4f1498f1c0314ba79ea2919bd7a02c4d",
    temperature=0.7,
    streaming=True
)

#创建解析器
# output_parser = CommaSeparatedListOutputParser()
output_parser = JsonOutputParser()
format_instruction = output_parser.get_format_instructions()

#创建prompt
prompt = PromptTemplate(
    template="列举多个常见的{topic}场景。{format_instruction}",
    input_variables=["topic"],
    partial_variables={"format_instruction": format_instruction}
)

chain = prompt | mode | output_parser

#流诗输出
for chunk in chain.stream({"topic":"消息队列应用"}):
    print(chunk)